package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.BatchSetProductOnSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Ts_BatchSetProductOnSale {
	private static BatchSetProductOnSaleCall batchSetProductOnSaleCall = new BatchSetProductOnSaleCall();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量上架商品");
//		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchSetProductOnSaleCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_001() {
		Logger.start(true,"上架多个商品");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
//			String productId1 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
//			String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			//update by wuwen AddMock 20201228
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			String productId2 = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			//先下架
			ProductManagerCallServiceV2.batchSetProductOffSale(Arrays.asList(productId1,productId2),sellerId);
			//在上架
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_002() {
		Logger.start(true,"上架活动商品");
		try {
			//String productId = ProductFactory.getActivityProduct(sellerId, sellerName).getProductId();
			
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			
			//获取商品ID
			//String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//update by wuwen AddMock 20201228
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			checkOnSaleTime(Arrays.asList(productId));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_004() {
		Logger.start(true,"过滤强制下架商品无法上架");
		try {
			String productId1 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			String productId2 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			//强制下架
			//String productId3 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			String productId3 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			ymtProductsIWapper.updateCheckStatus(productId3, -1);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "过滤强制下架商品 不能上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "过滤强制下架商品 不能上架");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_005() {
		Logger.start(true,"上架商品列表中有不属于当前用户的商品 无法上架");
		try {
			String productId =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId1,Arrays.asList(productId));
			
			Logger.verifyEquals(201, result.getInt("Code"), "返回code应为201");
			
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "上架商品列表中有不属于当前用户的商品 无法上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "上架商品列表中有不属于当前用户的商品 无法上架");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_006() {
		Logger.start(true,"库存为0商品能上架");
		try {
			String productId1 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			String productId2 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			//修改库存为0
			String productId3 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");	
			ProductManagerCallServiceV2.updateAllStockNumAs0(sellerId, productId3);

			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));

			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2,productId3));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_007() {
		Logger.start(true,"删除商品 无法上架");
		try {
			String productId =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			SellerProductCallService.batchDeleteProduct(sellerId, Arrays.asList(productId));
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId));
			
			Logger.verifyEquals(201, result.getInt("Code"), "返回code应为201");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_008() {
		Logger.start(true,"价格与物流方式不匹配 无法上架");
		try {
			String productId1 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			String productId2 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");	
			
			//价格与物流方式不匹配 无法上架
			String productId3 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");	
			
			ymtProductsIWapper.updateCatalogStatus(productId3, 5);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			

		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_BatchSetProductOnSale_009() {
		Logger.start(true,"商品分类（品类）有误 无法上架");
		try {
			String productId1 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			String productId2 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");	
			
			//商品分类（品类）有误 无法上架"
			String productId3 =  ProductManagerCallServiceV2.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");	
			
			ymtProductsIWapper.updateCategoryId(productId3, 1011);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "价格与物流方式不匹配 无法上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "价格与物流方式不匹配 无法上架");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_BatchSetProductOnSale_010() {
		Logger.start(true,"买手没有拼邮溯源发布权限，上架失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(288484);
			addProdReq.getProduct().setCatalogStatus(7);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateProductPackageDeliveryType(productId,2);
			//先下架
			ProductManagerCallServiceV2.batchSetProductOffSale(Arrays.asList(productId),288484);

			JSONObject result = batchSetProductOnSaleCall.call(288484,Arrays.asList(productId));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
			Date svalidStartbysql = (Date)productIDsqllist.get(0).get("validStart")	;
			Date svalidEndbysql = (Date)productIDsqllist.get(0).get("validEnd");
			AssertService.assertTime(YMTDateUtil.getDate(), svalidStartbysql, 60, "上架后 validStart");
			AssertService.assertTime(YMTDateUtil.getDate(), svalidEndbysql, 60,  "上架后 validEnd");
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_011() {
		Logger.start(true,"一个商品没有保税仓校验，上架成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			
			//String productId1 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//update by wuwen AddMock 20201228
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			addpro.getProduct().setBondedArea(0);
			//String productId2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String productId2 = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			//先下架
			ProductManagerCallServiceV2.batchSetProductOffSale(Arrays.asList(productId1,productId2),sellerId);
			//在上架
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_013() {
		Logger.start(true,"一个商品澳新版拼邮溯源，低于100元---贝海保税 ---上架成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(20323792);
			addProdReq.getProduct().setSecondCategoryId(1130);
			addProdReq.getProduct().setThirdCategoryId(1117);
			addProdReq.getProduct().setCatalogStatus(5);
			//String productId1 = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			addProdReq.getProduct().setBondedArea(0);
			//String productId2 = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String productId2 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			//先下架
			ProductManagerCallServiceV2.batchSetProductOffSale(Arrays.asList(productId1,productId2),20323792);
			//在上架
			JSONObject result = batchSetProductOnSaleCall.call(20323792,Arrays.asList(productId1,productId2));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			//checkOnSaleTime(Arrays.asList(productId1,productId2));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	public static void checkOnSaleTime(List<String> productIdList) throws Exception{
		checkOnSaleTime(productIdList,YMTDateUtil.getDate(),YMTDateUtil.getBeforeOrNextDate(7));
	}
	
	
	public static void checkOnSaleTime(List<String> productIdList, Date startDate , Date endDate ) throws Exception{
		for (String productId : productIdList) {
			//SQL YmtProducts
			@SuppressWarnings("rawtypes")
			List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
			Date svalidStartbysql = (Date)productIDsqllist.get(0).get("validStart")	;
			Date svalidEndbysql = (Date)productIDsqllist.get(0).get("validEnd");	
			AssertService.assertTime(startDate, svalidStartbysql, 600, "上架后 validStart");
			AssertService.assertTime(endDate, svalidEndbysql, 600,  "上架后 validEnd");
			
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
		}
	}
	
	
	
	
}
